/**
 * @ClassName: DicPriceDao
 * @author linsen
 * @date 2016-11-22
 */
package com.fzrskj.cloud.free.dic.dao;

import com.fzrskj.cloud.free.core.tool.CObjectTool;
import com.fzrskj.cloud.free.core.utils.CloudContextUtils;
import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.dic.bean.DicPrice;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DicPriceDao extends CObject {

    private IDbDrv db = null;

    public DicPriceDao() {
        db = CloudDbDrvUtils.getDbUtilsDrv();
    }

    //to free resource
    @Override
    public void dispose() {
        db.closeConnection();
    }

    //to add new record to database by bean
    public int addDicPrice(DicPrice obj) {
        //代理商数据隔离
        obj.setAgentId(CloudContextUtils.getAgentId());
        String sql = "insert into dic_price(hostType,chargeItem,parameter,price,agentId)" +
                "values(?,?,?,?,?);";
        Object[] prm = {obj.getHostType(), obj.getChargeItem(), obj.getParameter(), obj.getPrice(), obj.getAgentId()};
        int ret = db.executeSql(sql, prm);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to add new record to database by map
    public int addDicPrice(Map<String, Object> values) {
        //代理商数据隔离
        values.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.insertRecord("dic_price", values);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to delete table record by id
    public int delDicPrice(int id) {
        String sql = "delete from dic_price where id=?";
        Object[] params = {id};

        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.getAgentId());

        int ret = db.executeSql(sql, params);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to delete table record by cdt
    public int delDicPrice(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        List<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("delete from dic_price where 1=1 ");
        for (Map.Entry<String, Object> c : cdt.entrySet()) {
            if (c.getKey().equals("regionId") || c.getKey().equals("modelId")) {
                sb.append(" and LOCATE(?,parameter)>0");
                list.add(c.getValue());
            } else if (c.getKey().equals("cpu")) {
                sb.append(" and LOCATE(?,parameter)>0");
                list.add("\"cpu\":" + c.getValue());
            } else if (c.getKey().equals("memory")) {
                sb.append(" and LOCATE(?,parameter)>0");
                list.add("\"memory\":" + c.getValue());
            } else{
                sb.append(" and " + c.getKey() + "=?");
                list.add(c.getValue());
            }
        }

        Object[] params = new Object[list.size()];
        for (int i = 0; i < list.size(); i++) {
            params[i] = list.get(i);
        }

        int ret = db.executeSql(sb.toString(), params);
        this.setErrCode(db.getErrCode());
        return ret;
    }

    //to modify table record by values and condition
    public int modDicPrice(Map<String, Object> values, Map<String, Object> cdt) {

        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());

        int ret = db.modifyRecord("dic_price", values, cdt);
        String code = db.getErrCode();
        this.setErrCode(code);
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }

        return ret;
    }

    //to query dic_price item for detail
    public DicPrice queryDicPrice(int id) {
        Object[] params = {id};
        String sql = "select * from dic_price where id=?";

        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, CloudContextUtils.getAgentId());

        DicPrice ret = db.queryForBean(sql, DicPrice.class, params);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //to query DicPrice record count
    public int queryDicPrice(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());
        int ret = db.queryRecordCount("dic_price", cdt);
        this.setErrCode(db.getErrCode());
        return ret;
    }

    //to query DicPrice for record list with condition,if page or pagesize less than 0, means without page
    public List<DicPrice> queryDicPriceList(Map<String, Object> cdt, int page, int pagesize) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());
        List<DicPrice> ret = db.queryForBeans("dic_price", "*", "id desc", DicPrice.class, cdt, page, pagesize);
        String code = db.getErrCode();
        if (!code.equals(SysErr.CODE_SUCCESS)) {
            this.log(db.getMsg());
        }
        this.setErrCode(code);
        return ret;
    }

    //查询价格列表
    public List<DicPrice> queryDicPriceList(Map<String, Object> cdt) {
        //代理商数据隔离
        cdt.put("agentId", CloudContextUtils.getAgentId());
        List<Object> list = new ArrayList<>();
        StringBuilder sb = new StringBuilder("select * from dic_price where 1=1 ");

        for (Map.Entry<String, Object> c : cdt.entrySet()) {
            if (c.getKey().equals("regionId") || c.getKey().equals("modelId")) {
                sb.append(" and LOCATE(?,parameter)>0");
                list.add(c.getValue());
            } else {
                sb.append(" and " + c.getKey() + "=?");
                list.add(c.getValue());
            }
        }

        Object[] params = new Object[list.size()];
        for (int i = 0; i < list.size(); i++) {
            params[i] = list.get(i);
        }
        List<DicPrice> ret = db.queryForBeans(sb.toString(), DicPrice.class, params);
        this.setErrCode(db.getErrCode());
        return ret;
    }

    //查询价格
    public double queryPrice(String hostType, String chargeItem, String parameter) {
        return this.queryPriceAppointAgent(hostType, chargeItem, parameter, CloudContextUtils.getAgentId());
    }

    /**
     * 指定代理查询价格设置.
     * @param hostType 注册产品类型.
     * @param chargeItem 操作类型1新购2续费.
     * @param parameter 功能项编码.
     * @param agentId 代理ID.
     * @return 价格.
     */
    public double queryPriceAppointAgent(String hostType, String chargeItem, String parameter, Integer agentId) {
        double price = 0;
        String sql = "select price from dic_price where hostType=? and chargeItem=? and parameter=? ";
        Object[] params = {hostType, chargeItem, parameter};

        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, agentId);

        Object result = db.queryFieldValue(sql, double.class, params, 1);
        if (result != null) {
            price = Double.parseDouble(result.toString());
        }
        this.setErrCode(db.getErrCode());
        return price;
    }

    //模糊查询价格
    public DicPrice queryTremPrice(String hostType, String chargeItem, String parameter) {
        return this.queryTremPriceAppointAgent(hostType, chargeItem, parameter, CloudContextUtils.getAgentId());
    }

    public DicPrice queryTremPriceAppointAgent (String hostType, String chargeItem, String parameter, Integer agentId) {
        String sql = "select * from dic_price where hostType=? and chargeItem=? and LOCATE(?,parameter)>0 ";
        Object[] params = {hostType, chargeItem, parameter};

        //代理商数据隔离
        sql += " and agentId=? ";
        params = CObjectTool.objectArrAddValue(params, agentId);

        DicPrice ret = db.queryForBean(sql.toString(), DicPrice.class, params);
        this.setErrCode(db.getErrCode());
        return ret;
    }
}
